﻿using System;
using System.Collections.Generic;
using System.Linq;
using Nxg.Blog.Core.Documents;

namespace Nxg.Blog.Core.ViewProjections.Home {
    public class RecentBlogPostSummaryViewModel {
        public IEnumerable<BlogPostSummary> BlogPostsSummaries { get; set; }
    }

    public class BlogPostSummary {
        public string Title { get; set; }

        public string Link { get; set; }
    }

    public class RecentBlogPostSummaryBindingModel {
        public int Page { get; set; }
    }

    public class RecentBlogPostSummaryViewProjection : IViewProjection<RecentBlogPostSummaryBindingModel, RecentBlogPostSummaryViewModel> {
        private readonly DataContext _context;

        public RecentBlogPostSummaryViewProjection (DataContext context) {
            _context = context;
        }

        public RecentBlogPostSummaryViewModel Project (RecentBlogPostSummaryBindingModel input) {
            var titles = _context
                .Post
                .Where (x => x.PubDate <= DateTime.Now && x.Status == PublishStatus.Published)
                .OrderByDescending (x => x.PubDate)
                .Select (x => new BlogPostSummary {
                    Title = x.Title,
                        Link = x.GetLink ()
                }).Take (input.Page)
                .ToList ();
            return new RecentBlogPostSummaryViewModel { BlogPostsSummaries = titles };
        }
    }
}